using UnityEngine;
using System.Collections;

public class PlayerJumpState : State<PlayerControl>
{
    #region Singleton

    public static PlayerJumpState Instance = new PlayerJumpState();
    private PlayerJumpState() { }

    #endregion

    #region Attributes

    private bool headCollided; 
    private bool collideOnce;

    #endregion

    #region State

    public override void OnEnterState(PlayerControl behaviour)
    {
        this.headCollided = false;
        this.collideOnce = false;
        behaviour.targetSpeed = Vector3.zero;

        // Impulso inicial
        behaviour.targetSpeed.y = behaviour.jumpForce;

        //behaviour.gameObject.renderer.material.color = Color.yellow;        
    }

    public override void OnUpdateState(PlayerControl behaviour)
    {
        // Jump
        CollisionFlags cf = behaviour.characterController.collisionFlags & CollisionFlags.Above;
        this.headCollided = (cf == CollisionFlags.Above);

        if (this.headCollided && !this.collideOnce)
        {
            behaviour.targetSpeed.y = 0;
            this.collideOnce = true;
        }

        // Hang
        //if (Input.GetButton(Constants.HANG_BUTTON))
        //{
        //    RaycastHit hit = new RaycastHit();
        //    if (Physics.Raycast(behaviour.transform.position + behaviour.hangPointOffset, Vector3.right, out hit, 0.5f))
        //    {
        //        if (hit.collider.name.Contains(Constants.BORDER_NAME))
        //        {
        //            behaviour.transform.position = hit.transform.position - behaviour.hangPointOffset - new Vector3(0.7f, 0.0f, 0.0f);
        //            behaviour.State = PlayerHangState.Instance;
        //        }
        //    }
        //}

        // Hang
        if (Input.GetButton(Constants.HANG_BUTTON))
        {
            RaycastHit hit = new RaycastHit();
            if (Physics.Raycast(behaviour.hands.transform.position, behaviour.transform.forward, out hit, 0.5f))
            {
                Vector3 dist = hit.transform.position - behaviour.hands.transform.position;
                if (dist.magnitude < 1.0f)
                    behaviour.State = PlayerHangState.Instance;
                else
                    behaviour.targetSpeed = dist.normalized;
            }
        }

        //behaviour.Move(true, behaviour.jumpGravityPercentage);

        // Idle
        if (behaviour.characterController.isGrounded)
        {
            behaviour.State = PlayerIdleState.Instance;
            return;
        }
    }

    public override void OnFixedUpdateState(PlayerControl behaviour)
    {
    }

    public override void OnExitState(PlayerControl behaviour)
    {
        behaviour.targetSpeed = Vector3.zero;
    }

    public override void OnCollisionState(PlayerControl behaviour, Collision other)
    {
    }

    public override void OnTriggerState(PlayerControl behaviour, Collider other)
    {
        // Hang
        //if (Input.GetButton(Constants.HANG_BUTTON) && other.transform.position.y > behaviour.transform.position.y)
        //{
        //    behaviour.transform.position = other.transform.position - behaviour.hangPointOffset - new Vector3(0.7f, 0.0f, 0.0f);
        //    behaviour.State = PlayerHangState.Instance;
        //}
    }

    #endregion
}
